import { FormRules, FormItemRule } from 'element-plus';
import { reactive } from 'vue';

const requiredFields: Record<string, unknown> = {
  category: '杀菌剂',
  name: '42%三氯异氰尿酸可湿性粉剂',
  password: '',
  newPassword: '',
  mobileNumber: '',
  smsvcode: '',
  confirmPassword: '',
  registrationNumber: 'PD20095210',
  pesticideRegistrationType: 1,
  productionLicenseNumber: '农药生许（湘）0033',
  productStandardNumber: 'Q/DABV001-2018',
  dosageForm: '可湿性粉剂',
  entrusterEnterprise: '湖南神隆海洋生物工程有限公司',
  entrustedEnterprise: '湖南神隆海洋生物工程有限公司',
  warrantyPeriod: '两年',
  netContent: '500g/瓶',
  address: '湘潭九华经济技术开发银盖南路1号',
  tel: '0731-84692100',
  'instructions.features': '',
  productionType: '产品类型',
  productSpecificationCode: '产品规格代码',
  goodsName: '产品名',
  count: '数量',
  pesticideId: '产品id',
  legalRepresentative: '',
  corporationNumber: '',
  businessLicense: '',
};

const formRules: Record<string, FormItemRule[]> = {
  date: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a date',
      trigger: 'change',
    },
  ],

  type: [
    {
      type: 'array',
      required: true,
      message: 'Please select at least one activity type',
      trigger: 'change',
    },
  ],

  desc: [
    { required: true, message: 'Please input activity form', trigger: 'blur' },
  ],
  registrationNumber: [
    {
      pattern: /^(PD|LS|WP|WL|F[A-Z]).+$/,
      message: '登记证号格式错误，前2位字母请大写',
      trigger: 'blur',
    },
    {
      // 正则表达式解释：
      // (?:\d.*){6,}：非捕获组，匹配任意数字后跟任意字符，这个模式重复至少6次
      pattern: /(?:\d.*){6,}/,
      message: '登记证号中请确保存在6位数字',
      trigger: 'blur',
    },
  ],
};
/**
 * 添加字段
 * @param {Array<string>}params
 */
function addField(params: Record<string, unknown>) {
  //   const keys = Object.keys(params);
  //   for (let i = 0; i < keys.length; i++) {
  //     rules[keys[i]] = [
  //       { required: true, message: '不能为空', trigger: 'blur' },
  //     //   { validator: validateSqlInjection, trigger: 'blur' },
  //     ];
  //   }
  //   for (const item of params) {
  //     console.log(item);
  //     formRules[item] = [
  //       { required: true, message: '不能为空!', trigger: 'blur' },
  //     ];
  //   }
  for (const key in params) {
    if (Object.prototype.hasOwnProperty.call(params, key)) {
      if (formRules[key]) {
        formRules[key].push({
          required: true,
          message: '不能为空!',
          trigger: 'blur',
        });
      } else {
        formRules[key] = [
          { required: true, message: '不能为空!', trigger: 'blur' },
        ];
      }
    }
  }
}
addField(requiredFields);

export const rules = reactive<FormRules>(formRules);
// export default {
//   rules: rules,
// };
